package com.xunyin.modules.user.rest;

import com.xunyin.api.ApiResult;
import com.xunyin.common.bean.LocalUser;
import com.xunyin.common.interceptor.AuthCheck;
import com.xunyin.modules.log.AppLog;
import com.xunyin.modules.couponnew.service.mapper.MtUserCouponMapper;
import com.xunyin.modules.mtstore.domain.MtStaff;
import com.xunyin.modules.mtstore.service.StaffService;
import com.xunyin.modules.mtuserbalance.service.mapper.MtUserBalanceMapper;
import com.xunyin.modules.mtuserbalance.vo.MtUserBalanceVo;
import com.xunyin.modules.user.domain.YxUser;
import com.xunyin.modules.user.service.YxUserService;
import com.xunyin.modules.user.vo.YxUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户控制器
 * </p>
 *
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @since 2019-10-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "用户中心", tags = "用户:用户中心")
public class UserController {

    private final YxUserService yxUserService;

    private final MtUserCouponMapper mtUserCouponMapper;

    private final MtUserBalanceMapper mtUserBalanceMapper;

    private final StaffService staffService;

    /**
     * 用户资料
     */
    @AuthCheck
    @GetMapping("/userinfo")
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", response = YxUserQueryVo.class)
    public ApiResult<Object> userInfo() {
        YxUser yxUser = LocalUser.getUser();
        return ApiResult.ok(yxUserService.getNewYxUserById(yxUser));
    }

    /**
     * 获取用户的卡券数、总余额
     *
     * @return
     */
    @AuthCheck
    @GetMapping("/getUserStat")
    @ApiOperation(value = "获取用户的卡券数、总余额", notes = "获取用户的卡券数、总余额", response = YxUserQueryVo.class)
    public ApiResult<Object> getUserStat() {
        YxUser yxUser = LocalUser.getUser();
        Map<String, Object> data = new HashMap();
        // 卡券数
        Integer userCouponNum = mtUserCouponMapper.countUserCoupon(yxUser.getUid().intValue());
        data.put("userCouponNum", userCouponNum);
        // 用户总余额
        BigDecimal sumUserBalance = mtUserBalanceMapper.sumUserBalance(yxUser.getUid().intValue());
        if (sumUserBalance == null) {
            sumUserBalance = yxUser.getNowMoney();
        } else {
            sumUserBalance = sumUserBalance.add(yxUser.getNowMoney());
        }
        data.put("sumUserBalance", sumUserBalance);
        // 根据用户手机号,查询门店员工，是否是保洁员或是管理员
        List<MtStaff> staffList = staffService.queryStaffListByMobile(yxUser.getPhone());
        MtStaff mtStaff = new MtStaff();
        if (staffList != null && staffList.size() > 0) {
            mtStaff = staffList.get(0);
        }
        data.put("mtStaff", mtStaff);
        return ApiResult.ok(data);

    }

    /**
     * 获取用户门店余额列表
     */
    @AppLog(value = "获取门用户门店余额列表", type = 1)
    @AuthCheck
    @PostMapping("/user/storeBalanceList")
    @ApiOperation(value = "获取门用户门店余额列表", notes = "获取门用户门店余额列表")
    public ApiResult<List<MtUserBalanceVo>> getStoreBalanceList(HttpServletRequest request, @RequestBody Map<String, Object> param) {
        Integer storeId = (param.get("storeId") == null) ? 0 : Integer.parseInt(param.get("storeId").toString());
        YxUser yxUser = LocalUser.getUser();
        List<MtUserBalanceVo> list = mtUserBalanceMapper.getStoreBalanceList(yxUser.getUid().intValue());
        List<MtUserBalanceVo> reList = new ArrayList<MtUserBalanceVo>();
        MtUserBalanceVo mtUserBalanceVo = new MtUserBalanceVo();
        mtUserBalanceVo.setUserId(yxUser.getUid().intValue());
        mtUserBalanceVo.setStoreId(0);
        mtUserBalanceVo.setUserBalance(yxUser.getNowMoney());
        mtUserBalanceVo.setStoreName("平台通用");
        reList.add(mtUserBalanceVo);
        for (MtUserBalanceVo item : list) {
            // 如果查询对应门店的
            if (storeId > 0) {
                if (storeId == item.getStoreId()) {
                    reList.add(item);
                }
            } else {
                reList.add(item);
            }
        }
        log.info("storeBalanceList -> reList={}", reList.toString());
        return ApiResult.ok(reList);
    }

}
